#include "PCH.h"
#include "FileLoader.h"
using namespace DeviousRenderer;

FileLoader::FileLoader()
	: m_pData(NULL)
	, m_uiSize(0)
{

}

FileLoader::~FileLoader()
{
	Close();
}

bool FileLoader::Open(const std::wstring& fileName)
{
	Close();

	HANDLE hFile = CreateFile(fileName.c_str(),
								GENERIC_READ,
								FILE_SHARE_READ,
								NULL,
								OPEN_EXISTING,
								FILE_ATTRIBUTE_NORMAL,
								NULL);

	if (INVALID_HANDLE_VALUE == hFile)
	{
		return false;
	}
	// Get the file size
	LARGE_INTEGER FileSize = { 0 };

#if (_WIN32_WINNT >= _WIN32_WINNT_VISTA)
	FILE_STANDARD_INFO fileInfo;
	if ( !GetFileInformationByHandleEx( hFile, FileStandardInfo, &fileInfo, sizeof(fileInfo) ) ) {
		CloseHandle( hFile );
		return( nullptr );
	}
	FileSize = fileInfo.EndOfFile;
#else
	GetFileSizeEx( hFile, &FileSize );
#endif

	// File is too big for 32-bit allocation, so reject read
	if (FileSize.HighPart > 0) {
		CloseHandle( hFile );
		return( false );
	}

	m_uiSize = FileSize.LowPart;

	// create enough space for the file data
	m_pData = new char[ FileSize.LowPart ];

	if (!(m_pData)) {
		CloseHandle( hFile );
		Close();
		return( false );
	}

	// read the data in
	DWORD BytesRead = 0;
	if (!ReadFile( hFile,
		m_pData,
		FileSize.LowPart,
		&BytesRead,
		nullptr
		)) {
			CloseHandle( hFile );
			Close();
			return( false );
	}

	CloseHandle( hFile );

	if (BytesRead < FileSize.LowPart) {
		Close();
		return( false );
	}


	return( true );
}

bool FileLoader::Close( )
{
	if ( NULL != m_pData ) {
		delete [] m_pData;
		m_pData = NULL;
	}

	m_uiSize = 0;

	return( true );
}

//--------------------------------------------------------------------------------
char* FileLoader::GetDataPtr()
{
	return( m_pData );
}
//--------------------------------------------------------------------------------
unsigned int FileLoader::GetDataSize()
{
	return( m_uiSize );
}